package com.example.lx5_22.adapter

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.databinding.DataBindingUtil
import androidx.paging.PagingDataAdapter
import androidx.recyclerview.widget.DiffUtil.ItemCallback
import androidx.recyclerview.widget.RecyclerView.ViewHolder
import com.example.base.entity.MessageEntityItem
import com.example.lx5_22.R
import com.example.lx5_22.databinding.ItemMessageBinding

class MessageAdapter : PagingDataAdapter<MessageEntityItem,MessageAdapter.MessageViewHolder>(diff) {

    companion object{
        val diff = object : ItemCallback<MessageEntityItem>(){
            override fun areItemsTheSame(
                oldItem: MessageEntityItem,
                newItem: MessageEntityItem
            ): Boolean {
                return oldItem == newItem
            }

            override fun areContentsTheSame(
                oldItem: MessageEntityItem,
                newItem: MessageEntityItem
            ): Boolean {
                return oldItem.id == newItem.id
            }

        }
    }

    class MessageViewHolder(view:View) : ViewHolder(view){
        val binding = DataBindingUtil.bind<ItemMessageBinding>(view)
    }

    override fun onBindViewHolder(holder: MessageViewHolder, position: Int) {
        holder.binding?.mess = getItem(position)
        if (getItem(position)?.messageStatus=="处理中"){
            holder.binding?.tvSs?.setBackgroundResource(R.drawable.shap_message3)
        }else if(getItem(position)?.messageStatus=="已过期"){
            holder.binding?.tvSs?.setBackgroundResource(R.drawable.shap_message4)
        }else if(getItem(position)?.messageStatus=="已读"){
            holder.binding?.tvSs?.setBackgroundResource(R.drawable.shap_message2)
        }
        if(getItem(position)?.messageType=="车位管理"){
            holder.binding?.ivMessage?.setImageResource(R.drawable.ccc)
        }else if(getItem(position)?.messageType=="系统通知"){
            holder.binding?.ivMessage?.setImageResource(R.drawable.aaa)
        }else if(getItem(position)?.messageType=="物业服务"){
            holder.binding?.ivMessage?.setImageResource(R.drawable.bbb)
        }
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MessageViewHolder {
        val view =
            LayoutInflater.from(parent.context).inflate(R.layout.item_message, parent, false)
        return MessageViewHolder(view)
    }
}